package com.dognessnetwork.customer.controller.json;

import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

@RestController
@RequestMapping(value = "/js/device", name = "UserDevice")
public class DeviceController {
    private final   Logger  log =   LoggerFactory.getLogger(getClass());
    @Value("${device}")
    private String device;

    @Value("${clientServer}")
    private String clientServer;

    @Autowired
    private HttpSession session;

    /**
     * /js/device/get_all_device 获取用户的所有设备
     * 
     * @param username
     * @return
     */
    @ResponseBody
    @PostMapping("/get_all_device")
    public String get_all_device(String username) {
        log.info("\nget_all_device{}", username);
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("x-auth-token", getToken(username));
        String result = HttpUtil.post(device + "/devices/listByLoginUser", paramMap);
        log.info(result);

        return result;
    }

    /**
     * /js/device/get_all_pet 获取用户有的宠物
     * 
     * @param username
     * @return
     */
    @ResponseBody
    @PostMapping("/get_all_pet")
    public String get_all_pet(String username) {
        log.info("\nget_all_pet:{}", username);
        
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("x-auth-token", getToken(username));
        String result = HttpUtil.post(clientServer + "/pets/info/listPetByOwner", paramMap);
        log.info(result);

        return result;
    }
    
    public	String	getToken(String	key){
    	String	mine	=	session.getAttribute(key.substring(3))+"";
        JSONObject js = JSONUtil.parseObj(mine);
        log.info("\ngetToken:token[{}]", js.getStr("token"));

    	return	js.getStr("token");
    }
}
